package de.reichelt.fx;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.logging.Logger;
import javafx.scene.control.TextField;

public class FxIntegerValidator extends FxValidator {

  private Logger log;
  private NumberFormat nfmt = new DecimalFormat("#,##0");

  public FxIntegerValidator(FxFormField field) {
    super(field);
    if (log==null) {
      log = Logger.getLogger(this.getClass().getName());
    }
  }
  
  @Override
  public boolean isValid() throws Exception {
    log.fine("field.value="+field.getValue());
    String txt = ((TextField)field.getControl()).getText();
    if (txt==null || txt.isEmpty()) {
      return true;
    }
    txt = txt.replace(".", "").replace(",", ".");
    try {
      nfmt.parse(txt);
      return true;
    } catch (Exception ex) {
      return false;
    }
  }

  @Override
  public String getMessage() {
    return "Keine gültige Ganzzahl.";
  }
  
}
